const db = require("../../config/db");

module.exports = {
    getAttention(user_id, attention_name, create_time) {
        let sql = `select * from user_attention where user_id=${user_id} and attention_name="${attention_name}"`;
        return new Promise((reslove, reject) => {
            db.query(sql, (err, row) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "数据库查询出错",
                        err,
                    });
                } else {
                    if (row.length == 0) {
                        let insert_sql = `insert into user_attention(user_id,attention_name,create_time) values(${user_id},"${attention_name}","${create_time}")`;
                        db.query(insert_sql, (err2, row2) => {
                            if (err2) {
                                reject({
                                    code: -1,
                                    msg: "数据库插入失败",
                                    err2,
                                });
                            } else {
                                reslove({
                                    code: 200,
                                    msg: "插入成功",
                                    row2,
                                });
                            }
                        });
                    } else {
                        let del_sql = `DELETE FROM user_attention where user_id=${user_id} and attention_name="${attention_name}"`;
                        db.query(del_sql, (err3, row3) => {
                            if (err3) {
                                reject({
                                    code: -1,
                                    msg: "数据库删除失败",
                                    err3,
                                });
                            } else {
                                reslove({
                                    code: 200,
                                    msg: "数据库删除成功",
                                    row3,
                                });
                            }
                        });
                    }
                    // reslove({
                    //     code: 200,
                    //     msg: "数据库查询成功",
                    //     row,
                    // });
                }
            });
        });
    },
    selectAttention(user_id, attention_name) {
        let sql = `select * from user_attention where user_id=${user_id} and attention_name="${attention_name}"`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, row) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "查询数据库失败",
                        err,
                    });
                } else {
                    if (row.length == 0) {
                        resolve({
                            code: 200,
                            msg: "查询数据库成功",
                            row,
                            flat: 0,
                        });
                    } else {
                        resolve({
                            code: 200,
                            msg: "查询数据库成功",
                            row,
                            flat: 1,
                        });
                    }
                }
            });
        });
    },
    userAttention(user_id) {
        let sql = `select * from user_attention where user_id=${user_id}`;
        return new Promise((reslove, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                        msg: "数据库查询失败",
                    });
                } else {
                    reslove({
                        code: 200,
                        rows,
                        msg: "数据库查询成功",
                    });
                }
            });
        });
    },
    attentionManDetail(source) {
        let sql = `select * from news_detail where source="${source}"`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, row) => {
                if (err) {
                    reject({
                        code: 200,
                        err,
                        msg: "数据库查询失败",
                    });
                } else {
                    resolve({
                        code: 200,
                        row,
                        msg: "数据库查询成功",
                    });
                }
            });
        });
    },
};
